﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.IO;
using System.Net.Mail;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;

namespace EZCRS.HCMS.DataAcl
{
    /// <summary>
    /// Common 的摘要说明
    /// </summary>
    public class FGBCommon
    {
        public FGBCommon()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        #region 读取WebConfig节点
        /// <summary>
        /// 读取节点，无节点时返回默认字符串
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="defalut">默认字符串</param>
        /// <returns>节点值</returns>
        public static string GetConfiguration(string key,string defalut)
        {
            try
            {
                return System.Configuration.ConfigurationManager.AppSettings[key].ToString();
            }
            catch
            {
                return defalut;
            }
        }
        /// <summary>
        /// 读取节点，无节点时返回空字符串
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns>节点值</returns>
        public static string GetConfiguration(string key)
        {
              return GetConfiguration(key,"");
          }
        #endregion

          #region 加密数据
        internal static string GetaddMD5(string str)
          {
              
              MD5CryptoServiceProvider addMD5 = new MD5CryptoServiceProvider();
              return System.Text.ASCIIEncoding.ASCII.GetString(addMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(HttpContext.Current.Server.MachineName+ str)));
          }
          #endregion

          #region 通用功能
          #region 读取文件
          public static string ReadFile(string path)
          {
              try
              {
                  StreamReader sr = File.OpenText(path);
                  string Content = sr.ReadToEnd();
                  sr.Close();
                  sr = null;
                  return Content;
              }
              catch (Exception)
              {
                  return "文件不存在！";
              }
          }
          #endregion

          #region 发送邮件
          /// <summary>
          /// 发送邮件
          /// </summary>
          /// <param name="Content"></param>
          /// <param name="msg"></param>
          /// <returns></returns>
        public static bool SendEMail(string mailto, string subject, string Content, out string msg)
          {
              string mailserver = System.Configuration.ConfigurationManager.AppSettings["mailserver"].ToString();
              string mailuser = System.Configuration.ConfigurationManager.AppSettings["mailuser"].ToString();
              string mailpwd = System.Configuration.ConfigurationManager.AppSettings["mailpwd"].ToString();
              string mailfrom = "常客系统";
              string usermail = mailuser;
              string body = Content;
              return SendMail(mailto, "", usermail, mailfrom, subject, body, mailserver, mailuser, mailpwd, out msg);
              return true;
          }

          #endregion

          #region 发送邮件
          /// <summary>
          /// 发送邮件
          /// </summary>
          /// <param name="Content"></param>
          /// <param name="msg"></param>
          /// <returns></returns>
        public static bool SendEMail(string mailto, string emailalias, string mailfrom, string subject, string Content, out string msg)
          {
              string mailserver = System.Configuration.ConfigurationManager.AppSettings["mailserver"].ToString();
              string mailuser = System.Configuration.ConfigurationManager.AppSettings["mailuser"].ToString();
              string mailpwd = System.Configuration.ConfigurationManager.AppSettings["mailpwd"].ToString();
              string body = Content;
              return SendMail(mailto, "", mailfrom, emailalias, subject, body, mailserver, mailuser, mailpwd, out msg);
              return true;
          }

          #endregion

          #region  发送电子邮件(无附件)
          /// <summary>
          ///  发送电子邮件无附件
          /// </summary>
          /// <param name="mailTo">收件人</param>
          /// <param name="cc">抄送</param>
          /// <param name="mailFrom">发件人</param>
          /// <param name="subject">主题</param>
          /// <param name="mailPriority">邮件级别</param>
          /// <param name="mailFormat">邮件格式</param>
          /// <param name="encoding">编码</param>
          /// <param name="body">内容</param>
          /// <param name="mailServer">邮件服务器名称</param>
          /// <param name="user">用户名</param>
          /// <param name="password">密码</param>
          /// <param name="msg">输出信息</param>
          /// <returns></returns>
          public static bool SendMail(
              string mailTo,
              string cc,
              string mailFrom,
              string alias,
              string subject,
              string body,
              string mailServer,
              string user,
              string password, out string msg
              )
          {
              try
              {

                  System.Net.Mail.SmtpClient client = new SmtpClient(mailServer);
                  client.UseDefaultCredentials = false;
                  client.Credentials = new System.Net.NetworkCredential(user, password);
                  client.DeliveryMethod = SmtpDeliveryMethod.Network;

                  MailAddress MailFrom = new MailAddress(mailFrom, alias);


                  System.Net.Mail.MailMessage message = new MailMessage();

                  message.From = MailFrom;

                  string[] tempTo = mailTo.Split(',');
                  for (int i = 0; i < tempTo.Length; i++)
                  {
                      message.To.Add(tempTo[i]);
                  }
                  message.Subject = subject;
                  message.Body = body;
                  message.Priority = MailPriority.High;
                  message.BodyEncoding = Encoding.UTF8;
                  message.IsBodyHtml = true;
                  client.Send(message);
                  msg = "Email 发送成功";
                  return true;
              }
              catch (Exception ex)
              {
                  msg = "fail：" + ex.Message;
                  return false;
              }
          }
          #endregion

          #region 生成密码
          public static string CreatePassWord(int Len)
          {

              string password = "";
              string[] word = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
              int Max = word.Length;
              System.Random R = new Random(DateTime.Now.Millisecond);
              for (int i = 0; i < Len; i++)
              {
                  password += word[R.Next(Max)];
              }
              return password;

          }
          #endregion
          #endregion

        public static object IsNull(object o, Type type)
        {
            if (o == null||o.GetType()==typeof(DBNull))
            {
                if (type == typeof(int))
                {
                    return new Int32();
                }
                else if (type == typeof(DateTime))
                {
                    return new DateTime();
                }
                else if (type == typeof(bool))
                {
                    return new Boolean();
                }
                else if(type==typeof(string))
                {
                    return   string.Empty;
                }
                else if (type == typeof(byte))
                {
                    return new Byte();
                }
            }
            return o;
        }
      }


    public class InfoCommon
    {
        public static int j = 1;//中文

        /// <summary>
        /// 可用、禁用 两种状态
        /// </summary>
        public static string[] DoubleStatus = new String[] { "0,可用,Available", "1,禁用,Deniable" };

        #region 会员名或密码格式检查
        /// <summary>
        /// 检查用户名是否符合格式规范
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public static bool CheckUserName(string UserName)
        {
            Regex r = new Regex("[a-zA-Z_0-9]{4,15}");
            if (r.IsMatch(UserName))
                return true;
            else
                return false;
        }
        /// <summary>
        /// 检查密码是否符合格式规范
        /// </summary>
        /// <param name="UserPass"></param>
        /// <returns></returns>
        public static bool CheckUserPass(string UserPass)
        {
            Regex r = new Regex("[a-zA-Z_0-9]{5,15}");
            if (r.IsMatch(UserPass))
                return true;
            else
                return false;
        }
        #endregion

        #region  酒店星级、房型类型
        /// <summary>
        /// 酒店星级
        /// </summary>
        /// <returns></returns>
        public static DataTable HotelStar()
        {
            string[] LogData = new String[] { "1,一星级,One star", "2,二星级,Two star", "3,三星级,Three star", "4,四星级,Four star", "5,五星级,Five star", "0,无星级,None star" };

            DataTable dt = new DataTable();
            DataColumn dc1 = dt.Columns.Add("sid", Type.GetType("System.String"));
            DataColumn dc2 = dt.Columns.Add("sname", Type.GetType("System.String"));
            for (int i = 0; i < LogData.Length; i++)
            {
                DataRow dr = dt.NewRow();
                dr["sid"] = LogData[i].Split(',')[0];
                dr["sname"] = LogData[i].Split(',')[j];
                dt.Rows.Add(dr);
            }

            return dt;
        }

        /// <summary>
        /// 房型类型
        /// </summary>
        /// <returns></returns>
        public static DataTable RoomType()
        {
            string[] LogData = new String[] { "1,单人间,Single", "2,双人间,Double" };

            DataTable dt = new DataTable();
            DataColumn dc1 = dt.Columns.Add("rid", Type.GetType("System.String"));
            DataColumn dc2 = dt.Columns.Add("rname", Type.GetType("System.String"));

            for (int i = 0; i < LogData.Length; i++)
            {
                DataRow dr = dt.NewRow();
                dr["rid"] = LogData[i].Split(',')[0];
                dr["rname"] = LogData[i].Split(',')[j];
                dt.Rows.Add(dr);
            }
            return dt;
        }

        #endregion

        #region  性别(中英双语)
        public static Hashtable PeopleGender()
        {
            j--;

            Hashtable ht = new Hashtable();
            ht.Add(0, "保密,None".Split(',')[j]);
            ht.Add(1, "男,Male".Split(',')[j]);
            ht.Add(2, "女,Female".Split(',')[j]);

            return ht;
        }

        public static string PeopleGender(int PId)
        {
            return PeopleGender()[PId].ToString();
        }

        #endregion


        #region 吸烟喜好
        /// <summary>
        /// 吸烟喜好
        /// </summary>
        /// <returns></returns>
        public static Hashtable Smoking(int j)
        {
           
            Hashtable ht = new Hashtable();

            ht.Add(0, "无所谓,None".Split(',')[j]);
            ht.Add(1, "不吸烟,No smoking".Split(',')[j]);
            ht.Add(2, "吸烟,Smoking".Split(',')[j]);

            return ht;
        }

        public static string Smoking(int j,int SId)
        {
            return Smoking(j)[SId].ToString();
        }
        #endregion

 

        #region  语言(中英双语)
        public static Hashtable Language()
        {
            j--;
            Hashtable ht = new Hashtable();
            ht.Add(0, "中文,Chinese".Split(',')[j]);
            ht.Add(1, "英文,English".Split(',')[j]);

            return ht;
        }

        #endregion

        #region 信用卡类型
        /// <summary>
        /// 信用卡数据
        /// </summary>
        /// <returns></returns>
        public static DataTable Creditcard_Type()
        {

            string[] LogData = new String[] { "JC,Japan Credit,Japan Credit",
                                              "VI,Visa,Visa",
                                              "AX,American Express,American Express",
                                              "MC,MasterCard,MasterCard",
                                              "DC,Diners Club,Diners Club",
                                              "OO,其它类型,Others"};


            DataTable dt = new DataTable();
            DataColumn dc1 = dt.Columns.Add("cid", Type.GetType("System.String"));
            DataColumn dc2 = dt.Columns.Add("cname", Type.GetType("System.String"));

            for (int i = 0; i < LogData.Length; i++)
            {
                DataRow dr = dt.NewRow();
                dr["cid"] = LogData[i].Split(',')[0];
                dr["cname"] = LogData[i].Split(',')[j];

                dt.Rows.Add(dr);
            }
            return dt;
        }
        /// <summary>
        /// 根据编号获取信用卡类型
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public static string Creditcard_Type(string cid)
        {
            if (cid.Length != 2)
                return "";
            DataTable dt = Creditcard_Type();
            DataRow[] dr = dt.Select(" cid=" + cid);
            try
            {
                return dr[0][1].ToString();
            }
            catch
            {
                return "";
            }
        }
        #endregion

        #region 个人证件类型
        /// <summary>
        /// 个人证件类型
        /// </summary>
        /// <returns></returns>
        public static Hashtable Certificate_Type(int j)
        {
            Hashtable ht = new Hashtable();
            ht.Add(1, "身份证,身份证".Split(',')[j]);
            ht.Add(2, "护照,护照".Split(',')[j]);
            ht.Add(3, "军官证,军官证".Split(',')[j]);
            ht.Add(4, "士兵证,士兵证".Split(',')[j]);
            ht.Add(5, "武警证,武警证".Split(',')[j]);
            ht.Add(6, "其他,Others".Split(',')[j]);
            return ht;
        }
        public static string GetCertificate_Type(int j,int cid)
        {
            return Certificate_Type(j)[cid].ToString();
        }
        #endregion
    }
}
